package com.qdsg.ysg.doctor.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.ui.dialog.Item2Dialog;
import com.qdsg.ysg.doctor.ui.dialog.ItemDialog;
import com.rest.response.PrescriptionResponse;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.qdsg.ysg.doctor.R;

import org.jetbrains.annotations.NotNull;

/**
 * Created by HP on 2020/3/2.
 */

public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.Holder> {

    private AppCompatActivity context;
    private List<PrescriptionResponse.Second> list;
    private boolean isEdit = false, isCacheMould = false, isGuoYao = false;

    public SecondAdapter(AppCompatActivity context, List<PrescriptionResponse.Second> list) {
        this.context = context;
        this.list = list;
    }

    public SecondAdapter(AppCompatActivity context, List<PrescriptionResponse.Second> list, boolean isEdit, boolean isGuoYao) {
        this.context = context;
        this.list = list;
        this.isEdit = isEdit;
        this.isGuoYao = isGuoYao;
    }

    @NotNull
    @Override
    public SecondAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        SecondAdapter.Holder myHolder;
        myHolder = new Holder( LayoutInflater.from( context ).inflate( R.layout.item_second_diagnose, parent, false ) );
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NotNull SecondAdapter.Holder holder, final int position) {
        if (!isEdit) {
            holder.btn_del.setVisibility( View.GONE );
        }


        holder.btn_del.setOnClickListener( v -> {
            list.remove( position );
            notifyDataSetChanged();
        } );

        holder.pre2.setText( list.get( position ).preAppendName );
        holder.main2.setText( list.get( position ).diagnoseName );
        holder.tail2.setText( list.get( position ).tailAppendName );

        holder.pre2.setOnClickListener( v -> showSelectHeadPicDialog( 2, position ) );
        holder.main2.setOnClickListener( v -> showSelectHeadPicDialog2( position ) );
        holder.tail2.setOnClickListener( v -> showSelectHeadPicDialog( 4, position ) );
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    static class Holder extends RecyclerView.ViewHolder {

        TextView pre2, main2, tail2;
        ImageView btn_del;

        Holder(View itemView) {
            super( itemView );
            pre2 = itemView.findViewById( R.id.pre2 );
            main2 = itemView.findViewById( R.id.main2 );
            tail2 = itemView.findViewById( R.id.tail2 );
            btn_del = itemView.findViewById( R.id.btn_del );
        }
    }


    private void showSelectHeadPicDialog(final int type, final int position) {
        // 初始化点击头像弹出的对话框
        ItemDialog mItemDialog = ItemDialog.newInstance( type, "", isGuoYao );
        mItemDialog.setOnItemClickListener( (s, code) -> {
            list.get( position ).isMain = "0";
            if (type == 2) {  //前缀
                list.get( position ).preAppendName = s;
                list.get( position ).preAppendId = code;
                notifyDataSetChanged();
            } else if (type == 4) {
                list.get( position ).tailAppendName = s;
                list.get( position ).tailAppendId = code;
                notifyDataSetChanged();
            }
        } );
        mItemDialog.show( context.getSupportFragmentManager(), "mItemDialog" );
    }




    private void showSelectHeadPicDialog2(final int position) {
        // 初始化点击头像弹出的对话框
        Item2Dialog mItem2Dialog = Item2Dialog.newInstance( 3 );
        mItem2Dialog.setOnItemClickListener( (s, id) -> {
            list.get( position ).diagnoseName = s;
            list.get( position ).icdId = id;
            notifyDataSetChanged();
        } );
        mItem2Dialog.show( context.getSupportFragmentManager(), "mItemDialog" );
    }
}
